<?php
declare (strict_types=1);

namespace app\admin\service\user;

use app\admin\logic\user\Account as AccountLogic;
use app\admin\logic\user\Group as GroupLogic;
use app\common\service\BaseService;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageForm;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\facade\Request;

/**
 * 用户接口类
 */
class Account extends BaseService
{

    /**
     * @Inject()
     *
     * @var AccountLogic
     */
    protected $accountLogic;
    /**
     * @Inject()
     *
     * @var GroupLogic
     */
    protected $groupLogic;

    /**
     * 用户登录
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-12
     */
    public function login()
    {
        $data = Request::post();
        $user = $this->accountLogic->login($data);

        return $this->parseToData($user->toArray(), 1, '登录成功');
    }

    /**
     * 创建后台用户
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-12
     */
    public function createUser()
    {
        if (Request::isPost()) {
            $data = Request::post();
            $user = $this->accountLogic->createUser($data);

            return $this->parseToData($user->toArray(), 1, '创建成功');
        }
        $group = $this->groupLogic->getGroupSelectWithout2();
        // 创建课程界面配置
        // 定义字段
        $keyList = [
            'user_name' => FormItem::make()->title('用户名')->placeholder('请输入用户名')->required(),
            'password'  => FormItem::make('inputPassword')->title('登录密码')->placeholder('登录密码')->required(),
            'phone'     => FormItem::make()->title('手机号'),
            'group'     => FormItem::make('checkbox')->title('角色')->options($group),
            // 'status'    => FormItem::make('slideSwitch')->title('账号状态')->activeText('正常')->activeValue(1)->inactiveText('禁用')->inactiveValue(0)->required(),
            // 'intro'     => FormItem::make('editor')->title('简介'),
        ];
        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->pageKey = $keyList;

        return $form->send();
    }

    /**
     * 编辑后台用户
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-12
     */
    public function editUser($id)
    {
        if (Request::isPut()) {
            $data = Request::put();
            $user = $this->accountLogic->editUser($data);
            return $this->parseToData($user->toArray(), 1, '编辑成功');
        }
        $datas  = $this->accountLogic->with(['group_ids'])->where('id', $id)->findOrEmpty();
        $group2 = $datas->group_ids && in_array(2, $datas->group_ids);
        $group  = $this->groupLogic->getGroupSelectWithout2();
        // 创建课程界面配置
        // 定义字段
        $keyList = [
            'user_name' => FormItem::make()->title('用户名')->placeholder('请输入账号')->required(),
            'password'  => FormItem::make('inputPassword')->title('登录密码')->placeholder('登录密码')->help('1. 不填写则不修改<br />2. 密码须为数字、字母和英文标点符号组成的6-20位字符'),
            'phone'     => FormItem::make()->title('手机号')->required(),
            // 'status'    => FormItem::make('slideSwitch')->title('账号状态')->activeText('正常')->activeValue(1)->inactiveText('禁用')->inactiveValue(0)->required(),
        ];
        if (!$group2) $keyList['group_ids'] = FormItem::make('checkbox')->title('角色')->options($group);

        // 设置表单
        $form = app(PageForm::class);
        // 表单字段
        $form->datas   = $datas;
        $form->pageKey = $keyList;
        return $form->send();

    }

    /**
     * 编辑后台用户指定的字段
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-12
     */
    public function editUserField($id)
    {
        $data = Request::put();
        $user = $this->accountLogic->editUserField($id, $data);
        return $this->parseToData($user->toArray(), 1, '编辑成功');
    }

    /**
     * 删除后台用户
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-12
     */
    public function deleteUser($id)
    {
        $this->accountLogic->deleteUser($id);
        return $this->parseToData([], 1, '删除成功');
    }

    /**
     * 用户列表
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-13
     */
    public function index()
    {
        // 搜索功能
        $map = [];
        Request::has('user_name', 'get', true) && $map[] = ['user_name', 'like', '%' . Request::get('user_name/s') . '%'];
        Request::has('phone', 'get', true) && $map[] = ['phone', 'like', '%' . Request::get('phone/s') . '%'];
        if (Request::has('status', 'get', true) && Request::get('status/d', -1) !== -1) {
            $map[] = ['status', '=', Request::get('status/d')];
        }

        $group_id = Request::has('group', 'get', true) ? Request::get('group/d') : 0;
        $data     = $this->accountLogic->getUserList($map, $group_id);

        $group = $this->groupLogic->getGroupSelect();
        // 定义字段
        $keyList = [
            'user_name'       => ListItem::make()->title('账号'),
            'phone'           => ListItem::make()->title('手机号'),
            'group'           => ListItem::make()->title('角色'),
            'last_login_time' => ListItem::make()->title('最近登录时间'),
            'update_time'     => ListItem::make()->title('更新时间'),
            'status'          => ListItem::make('custom')->title('状态'),
        ];

        $searchKey = [
            'user_name' => FormItem::make('input')->title(false)->placeholder('账号')->maxlength(30),
            'phone'     => FormItem::make('input')->title(false)->placeholder('手机号')->maxlength(30),
            'group'     => FormItem::make('select')->title(false)->placeholder('角色')->options($group),
            'status'    => FormItem::make('select')->title(false)->placeholder('账号状态')->options([
                ['title' => '不限制', 'value' => -1],
                ['title' => '正常', 'value' => 1],
                ['title' => '禁止', 'value' => 0],
            ])
        ];

        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey = $keyList;
        // 自定义排序
        // $list->pageKeySort = ['update_time DESC', 'user_name'];
        $list->searchKey     = $searchKey;
        $list->searchKeySort = [];
        $list->datas         = $data;
        return $list->send();
    }

    /**
     * 获取指定用户的用户组
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-20
     */
    public function getUserGroups($id)
    {
        $userGroups = $this->accountLogic->getUserGroups($id);

        return $this->parseToData($userGroups, 1, '获取成功');
    }

    /**
     * 获取指定用户的用户组
     * Author   亓官雨树<lucky.max@foxmail.com>
     * Date 2020-03-20
     */
    public function setUserGroups($id)
    {
        $groups     = Request::put('groups');
        $userGroups = $this->accountLogic->setUserGroups($id, $groups);

        return $this->parseToData($userGroups, 1, '编辑成功');
    }

    /**
     * 管理员信息编辑
     * Author: Martinsun <syh@sunyonghong.com>
     * Date: 2021/2/8
     *
     * @return \eduline\admin\page\interfaces\think\Response|\think\Response
     */
    public function adminer()
    {
        $id = \request()->admin_user_id;
        if (Request::isPut()) {
            $data = Request::put();
            $user = $this->accountLogic->editUser($data);
            return $this->parseToData($user->toArray(), 1, '编辑成功');
        }
        $datas = $this->accountLogic->where('id', $id)->findOrEmpty();
        // 创建课程界面配置
        // 定义字段
        $keyList = [
            'user_name' => FormItem::make()->title('用户名')->placeholder('请输入账号')->required(),
            'password'  => FormItem::make('inputPassword')->title('登录密码')->placeholder('登录密码')->help('1. 不填写则不修改<br />2. 密码须为数字、字母和英文标点符号组成的6-20位字符'),
            'phone'     => FormItem::make()->title('手机号')->required(),
        ];
        // 设置表单
        $form = new PageForm();
        // 表单字段
        $form->datas   = $datas;
        $form->pageKey = $keyList;

        return $form->send();
    }
}
